; ----------------------------------------------------------------------------------------- ;
; AHK ScriptTester v1.45 (by Wouther)
; ----------------------------------------------------------------------------------------- ;
; Version Info:
; v1.45   Checkbox added for quick enabling/disabling filter.
; v1.40   Tab added for list with filtercommands. Some structural changes to make the filter
;         work. Check to make sure the temp-file exists, before running it in sub
;         'ButtonLaunch'. It now checks for commands from the filterlist and warns the user
;         when needed.
; v1.30   Store extension and location of gui (incl. tweaking) in an ini-file. Gui added for
;         editing settings (incl. button to open this gui).
; v1.25   Create gui outside the visible area of the screen, changed slashes in paths into
;         backslashes, credits added.
; v1.20   Textbox added for variable file-extension.
; v1.10   Better gui alignement, 'exit' button changed to cross, dropdown-box for automatic
;         getting/launching code, gui blinks yellow and closes alert-box.
; v1.00   Initial version.
; ----------------------------------------------------------------------------------------- ;
; Credits:
; Wouther: idea of toolbar, scripting
; MarkyMark: thanks for noticing the slash-problem (fixed in v1.25) and idea of an ini-file
; HugoV: thanks for noticing the slash-problem (fixed in v1.25) and information on ini-files
; Engunneer: thanks for the idea on bad/dangerous code (not implemented yet)
; ----------------------------------------------------------------------------------------- ;

#Persistent
#NoTrayIcon
Gui, +LastFound +AlwaysOnTop -MaximizeBox -MinimizeBox +ToolWindow -Caption +Border
Gui, 2:+LastFound +AlwaysOnTop -MaximizeBox -MinimizeBox +ToolWindow -Caption +Border
Gui, Color, White
Gui, 2:Color, White

ClipSaved=
StandardFilterCommands=DllCall|FileAppend|FileCopy|FileCopyDir|FileCreateDir|FileCreateShortcut|FileDelete|FileInstall|FileMove|FileMoveDir|FileRecycle|FileRecycleEmpty|FileRemoveDir|FileSetAttrib|FileSetTime|IniDelete|IniWrite|Process|RegDelete|RegWrite|Run|RunWait|Shutdown|UrlDownloadToFile|WinClose|WinKill

INIReadAuto()
INIWriteAuto()

Gui, Add, Button, x5 y5 w50, Save
Gui, Add, Button, x60 y5 w50, Edit
Gui, Add, Button, x115 y5 w50, Get
Gui, Add, Button, x170 y5 w50, Launch
Gui, Add, DropDownList, x225 y6 w90 vAuto, None|Get|Get & Launch||
Gui, Add, Edit, r1 vExtension x320 y6 w25, %INI_ext%
Gui, font, s8, Verdana
Gui, Add, Text, x349 y1 gButtonExit, X
Gui, Add, Text, x349 y15 gButtonSettings, S

Gui,2:Default
Gui, Add, Tab, x5 y5 w350 h190, Filter results|Filter settings|Appearance|Restore
Gui, Tab, Filter results
Gui, Add, ListView, x15 y35 r9 w330 vFilterLV -Multi, ln|Command|Line
Gui, Tab, Filter settings
Gui, Add, ListView, x15 y35 r9 w175 vFilterSettingsLV -Multi, Command
GetFilterCommands()
Gui, Add, Button, x200 y35 w140 gButtonDeleteCommand, Delete command
Gui, Add, Checkbox, vFilterEnabled Checked%FilterEnabled%, Enable Filter
Gui, Add, Edit, x200 y135 w140 vnewCommand
Gui, Add, Button, x200 y160 w140 gButtonNewCommand Default, New command
Gui, Tab, Appearance
Gui, Add, Edit, x15 y40 w45 vSettingsLocH
Gui, Add, UpDown, Range1-3, %INI_locH%
Gui, Add, Text, x80 y45, Horizontal alignment (1=left, 2=center, 3=right)
Gui, Add, Edit, x15 y65 w45 vSettingsLocV
Gui, Add, UpDown, Range1-2, %INI_locV%
Gui, Add, Text, x80 y70, Vertical alignment (1=bottom, 2=top)
Gui, Add, Edit, x15 y90 w45 vSettingsLocX
Gui, Add, UpDown, Range-100-100, %INI_locX%
Gui, Add, Text, x80 y95, Horizontal tweaking (in pixels)
Gui, Add, Edit, x15 y115 w45 vSettingsLocY
Gui, Add, UpDown, Range-100-100, %INI_locY%
Gui, Add, Text, x80 y120, Vertical tweaking (in pixels)
Gui, Tab, Restore
Gui, Add, Button, x70 y45 w215 gButtonDefault, Click to restore the default settings
Gui, Tab
Gui, Add, Text, x349 y1 gButtonExitSettings, X



Gui,1:Default
Gui, Show, x0 y-100 w360, AHK ScriptTester

WinWait, AHK ScriptTester
IfWinNotActive, AHK ScriptTester
   WinActivate, AHK ScriptTester
CalcGui()
WinMove, AHK ScriptTester,, guiX, guiY

;Detecting, acting
Loop
{
   WinWait, The page at http://www.autohotkey.com says:
   Gui, Color, Yellow
   Sleep, 500
   Gui, Color, White
   WinClose
   GuiControlGet, Auto
   If Auto:="Get" OR Auto:="Get & Launch"
      Gosub, ButtonGet
   If Auto:="Get & Launch"
      Gosub, ButtonLaunch
}

return


ButtonSave:
FormatTime, NowFormatted,, dd.MM.yy_HH_mm_ss
GuiControlGet, ext,, Extension
FileType = %ext%-files (*.%ext%)
if ext=ahk
   FileType = Autohotkey (*.ahk)
FileSelectFile, PathSaved, S16, ::{20d04fe0-3aea-1069-a2d8-08002b30309d}\SCRIPT_%NowFormatted%.%ext%, Save the script, %FileType%
If ErrorLevel=0
{
   FileCopy, %A_Temp%\clipboard.%ext%, %PathSaved%, 1
   Run, notepad.exe %PathSaved%
}
return

ButtonEdit:
GuiControlGet, ext,, Extension
Run, notepad.exe %A_Temp%\clipboard.%ext%
return

ButtonGet:
Transform, ClipSaved, Unicode
IfExist, %A_Temp%\clipboard.*
   FileDelete, %A_Temp%\clipboard.*
GuiControlGet, ext,, Extension
FileAppend, %ClipSaved%, %A_Temp%\clipboard.%ext%
ClipSaved=
return

ButtonLaunch:
IfNotExist, %A_Temp%\clipboard.%ext%
   Exit
Gui, 2:Submit, NoHide
If (FilterEnabled=1)
{
   Gui, Color, 55AAFF
   Sleep, 500
   Gui, 2:Default
   Clean := 1
   Gui, ListView, FilterLV
   LV_Delete()
   StringSplit, FilterCommandsArray, FilterCommands, |
   Loop, read, %A_Temp%\clipboard.%ext%
   {
      LineNumber := A_Index
      Loop, %FilterCommandsArray0%
      {
         CheckCommand := FilterCommandsArray%A_Index%
         IfInString, A_LoopReadLine, %CheckCommand%
         {
            LV_Add("", LineNumber, CheckCommand, A_LoopReadLine)
            Clean := 0
         }
      }
   }
   Gui, 2:Default
   LV_ModifyCol()
   Gui, 1:Default
   Gui, Color, White
   If (Clean=1)
   {
      GuiControlGet, ext,, Extension
      Run, %A_Temp%\clipboard.%ext%
   }
   Else
   {
      MsgBox, 51, Filter dialog, The code you tried to launch, contains commands from the filter.`nDo you still want to launch it? Click 'No' to view the filter results.
      IfMsgBox, Yes
      {
         GuiControlGet, ext,, Extension
         Run, %A_Temp%\clipboard.%ext%
      }
      IfMsgBox, No
      {
         ;View filter results
         IfWinNotExist, AHK ScriptTester Settings
         {
            WinGetPos,,, w1, h1, AHK ScriptTester
            w1:=w1-2
            Gui, 2:Show, x0 y-210 w%w1% h200, AHK ScriptTester Settings
            ;Using INI-values
            If INI_locV=1
               settingsY:=guiY-201
            If INI_locV=2
               settingsY:=guiY+h1-1
            If INI_locH=1 OR INI_locH=2
               correctionX:=5
            Else
               correctionX:=0
            WinMove, AHK ScriptTester Settings,, guiX-correctionX, settingsY
         }
         Gui, 2:Default
         GuiControl, Choose, SysTabControl321, 1
         Gui, 1:Default
      }
   }
}
else
{
   GuiControlGet, ext,, Extension
   Run, %A_Temp%\clipboard.%ext%
}
return

ButtonSettings:
IfWinExist, AHK ScriptTester Settings
{
   Gui, 2:Submit
   INI_locH:=SettingsLocH
   INI_locV:=SettingsLocV
   INI_locX:=SettingsLocX
   INI_locY:=SettingsLocY
   INIWriteAuto()
   CalcGui()
   WinMove, AHK ScriptTester,, guiX, guiY
   Exit
}
WinGetPos,,, w1, h1, AHK ScriptTester
w1:=w1-2
Gui, 2:Show, x0 y-210 w%w1% h200, AHK ScriptTester Settings
;Using INI-values
If INI_locV=1
   settingsY:=guiY-201
If INI_locV=2
   settingsY:=guiY+h1-1
If INI_locH=1 OR INI_locH=2
   correctionX:=5
Else
   correctionX:=0
WinMove, AHK ScriptTester Settings,, guiX-correctionX, settingsY
return

ButtonExitSettings:
Gui, 2:Submit
INI_locH:=SettingsLocH
INI_locV:=SettingsLocV
INI_locX:=SettingsLocX
INI_locY:=SettingsLocY
INIWriteAuto()
CalcGui()
WinMove, AHK ScriptTester,, guiX, guiY
return

ButtonDefault:
MsgBox, 52, Restore default settings, Are you sure you want to restore the default settings?
IfMsgBox, Yes
{
   FileDelete, settings.ini
   Reload
}
return

ButtonNewCommand:
Gui, ListView, FilterSettingsLV
GuiControlGet, newCommand
IfInString, FilterCommands, %newCommand%
   MsgBox, 16, Adding new command, Command already exists!
else
{
   FilterCommands=%newCommand%|%FilterCommands%
   IniWrite, %FilterCommands%, settings.ini, Filter, commands
   LV_Delete()
   GetFilterCommands()
}
return

ButtonDeleteCommand:
Gui, ListView, FilterSettingsLV
ControlGet, deleteCommand, List, Selected, SysListView322, AHK ScriptTester Settings
If deleteCommand=
   return
MsgBox, 52, Delete command, Are your sure you want to delete the command '%deleteCommand%' from the filterlist?
IfMsgBox, Yes
{
   IfInString, FilterCommands, %deleteCommand%|
      StringReplace, FilterCommands, FilterCommands, %deleteCommand%|
   else
      StringReplace, FilterCommands, FilterCommands, |%deleteCommand%
   IniWrite, %FilterCommands%, settings.ini, Filter, commands
   LV_Delete()
   GetFilterCommands()
}
return

CalcGui()
{
   global
   WinGetPos,,, w1, h1, AHK ScriptTester
   WinGetPos,,,, h2, ahk_class Shell_TrayWnd
   ;Using INI-values
   If INI_locH=1
      guiX:=INI_locX
   If INI_locH=2
      guiX:=(A_ScreenWidth/2)-(w1/2)+INI_locX
   If INI_locH=3
      guiX:=A_ScreenWidth-w1+INI_locX
   If INI_locV=1
      guiY:=A_ScreenHeight-h1-h2+INI_locY
   If INI_locV=2
      guiY:=h2+INI_locY  ;Vertical tweaking is almost always needed. The height of the taskbar will be used.
}

GetFilterCommands()
{
   global
   IniRead, FilterCommands, settings.ini, Filter, commands
   If (FilterCommands="ERROR")
   {
      MsgBox, 16, Error, Error retrieving variable FilterCommands from settings.ini
      Exit
   }
   StringSplit, Filter, FilterCommands, |
   Gui, ListView, FilterSettingsLV
   Loop, %Filter0%
   {
      LV_Add("",Filter%A_Index%)
   }
}

INIReadAuto()
{
   global
   IniRead, INI_ext, settings.ini, Basic, ext
   If INI_ext=ERROR
      INI_ext=ahk
   IniRead, INI_locH, settings.ini, Location, locH ;Horizontal alignement (1=left, 2=center, 3=right)
   If INI_locH=ERROR
      INI_locH=1
   IniRead, INI_locV, settings.ini, Location, locV ;Vertical alignement (1=bottom, 2=top)
   If INI_locV=ERROR
      INI_locV=1
   IniRead, INI_locX, settings.ini, Location, locX ;Horizontal tweaking (in pixels)
   If INI_locX=ERROR
      INI_locX=0
   IniRead, INI_locY, settings.ini, Location, locY ;Vertical tweaking (in pixels)
   If INI_locY=ERROR
      INI_locY=0
   IniRead, FilterCommands, settings.ini, Filter, commands
   If FilterCommands=ERROR
      FilterCommands:=StandardFilterCommands
   IniRead, FilterEnabled, settings.ini, Filter, enabled
   If FilterEnabled=ERROR
      FilterEnabled:=1
}

INIWriteAuto()
{
   global
   Gui, 1:Default
   GuiControlGet, ext,, Extension
   IniWrite, %ext%, settings.ini, Settings, ext
   IniWrite, %INI_locH%, settings.ini, Location, locH
   IniWrite, %INI_locV%, settings.ini, Location, locV
   IniWrite, %INI_locX%, settings.ini, Location, locX
   IniWrite, %INI_locY%, settings.ini, Location, locY
   IniWrite, %FilterCommands%, settings.ini, Filter, commands
   IniWrite, %FilterEnabled%, settings.ini, Filter, enabled
}

ButtonExit:
GuiClose:
IfExist, %A_Temp%\clipboard.*
   FileDelete, %A_Temp%\clipboard.*
INIWriteAuto() ;Changing INI-file
GUI_ID := WinExist()
DllCall("AnimateWindow","UInt",GUI_ID,"Int",1000,"UInt","0x90000") ;Fade out
ExitApp
return